home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*---------------------------------------------------------------------------
- * mnls_wchar.c : sample program which shows the use of wide characters
- * for string manipulation which require locating specific
- * characters
- * Note : this uses MNLS way of cataloging
- *
- * this example reads a command line string and outputs two interleaved
- * strings (not useful in real life, but shows how to locate and
- * manipulate strings made of wide characters).
- *
- * Author : Yusuf Attarwala
- * Date : July 93
- *
- *---------------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdarg.h>
- #include <ctype.h>
- #include <string.h>
- #include <locale.h>
- #include <wctype.h>
- #include <pfmt.h>
-
- void
- usage(pname)
- char *pname;
- {
- /* MM_ERROR is one of the flags,
- othe flags can be found in pfmt.h
- */
-
- (void) pfmt(stderr, MM_ERROR, ":1:Usage: %s string\n", pname);
- }
-
- myExit()
- {
- /* cleanup and exit */
- exit();
- }
-
- int
- main(argc, argv)
- register argc;
- char **argv;
- {
- char *prog;
- register int c;
- char label[20];
- register wchar_t *s,*o,*p;
- wchar_t *first,*second;
- int i,n;
-
- /* i18n support */
- /* establish a locale, cause locale database to be read in */
- /* passing empty string will cause setlocale to look
- for an environment variable LANG */
-
- (void)setlocale(LC_ALL, "");
-
- /* open a catalogue for messages, etc */
- prog = (prog = strrchr(argv[0], '/')) ? prog+1 : argv[0];
- (void) setcat("mnls_wchar.cat");
- (void) sprintf(label, "WC:%s", prog);
- (void) setlabel(label);
-
- /* sanity check */
- if (argc < 2) {
- usage(argv[0]);
- myExit();
- }
-
- /* allocate memory for the wide character string */
- n = strlen(argv[1]);
- if( !(s = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
- (void) pfmt(stderr, MM_ERROR, ":2:Could not allocate memory\n");
- myExit();
- }
-
- /* convert string to wchar_t */
- if(mbstowcs(s, argv[1], n) < 0) {
- (void) pfmt(stderr, MM_ERROR, ":3:Error in creating wide characters\n");
- myExit();
- }
-
- /* allocate memory for second string */
- if( !(p = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
- (void) pfmt(stderr, MM_ERROR, ":2:Could not allocate memory\n");
- myExit();
- }
-
- o = s;
-
- first = o;
- second = p;
-
- /* collect odd and even letters in different strings */
- for(i=0; *s; s += 2,i++) {
- if (!*(s-1) && i > 1) break; /* don't skip the null terminator */
- *o++ = *s;
- *p++ = *(s+1);
- }
-
- /* null terminators */
- mbtowc (o,"\0",1);
- mbtowc (p,"\0",1);
-
- /* print out the interleaved strings */
- (void) pfmt(stderr, MM_INFO, ":4:Here's the Output : ");
-
- putws(first);
- putws(second);
- }
-